package com.openlogic.demo.controller;

import java.util.Arrays;

import java.util.List;

import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Delete;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Path;
import org.noear.solon.annotation.Post;
import org.noear.solon.annotation.Put;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.annotation.Validated;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.openlogic.common.core.enums.BusinessType;
import com.openlogic.common.core.utils.ExcelUtil;
import com.openlogic.common.core.web.controller.BaseController;
import com.openlogic.common.core.web.domain.PageData;
import com.openlogic.common.datasource.annotation.QzLog;
import com.openlogic.common.rabbitmq.util.RabbitMqUtil;
import com.openlogic.demo.domain.DemoTest;
import com.openlogic.demo.service.IDemoTestService;

/**
 * 演示 操作处理
 * @author xm
 */
@Controller
@Mapping("test")
public class DemoTestController extends BaseController {

    @Inject
    IDemoTestService demoTestService;

    @Inject
    RabbitMqUtil rabbitMqUtil;
    
    /**
     * 查询演示列表
     */
    @AuthPermissions("demo:test:list")
    @Get
    @Mapping("list")
    public PageData<DemoTest> list(Page<DemoTest> page, DemoTest demoTest) {
    	try {
			rabbitMqUtil.convertAndSend("openlogic_demo_echange", "demo", "你好测试发送消息");
		} catch (Exception e) {
			e.printStackTrace();
		}
    		QueryWrapper qw = getQW(demoTest);
        Page<DemoTest> result = demoTestService.page(page, qw);
        
        String msg = rabbitMqUtil.receiveMessageByQueueName("demo_test", "openlogic_demo_echange", "demo");
        System.out.println("msg==="+msg);
        
        return getPageData(result);
    }

    /**
     * 导出演示列表
     */
    @QzLog(title = "演示", businessType = BusinessType.EXPORT)
    @AuthPermissions("demo:test:export")
    @Post
    @Mapping("export")
    public void export(DemoTest demoTest) {
        QueryWrapper qw = getQW(demoTest);
        List<DemoTest> list = demoTestService.list(qw);
        ExcelUtil.export(list, "测试.xls");
    }

    /**
     * 查询演示详情
     */
    @AuthPermissions("demo:test:query")
    @Get
    @Mapping("{id}")
    public DemoTest info(@Path Long id) {
        return demoTestService.getById(id);
    }

    /**
     * 新增演示
     */
    @QzLog(title = "演示", businessType = BusinessType.INSERT)
    @AuthPermissions("demo:test:add")
    @Post
    @Mapping
    public Result<Void> add(@Body @Validated DemoTest demoTest) {
        return toResult(demoTestService.save(demoTest));
    }

    /**
     * 修改演示
     */
    @QzLog(title = "演示", businessType = BusinessType.UPDATE)
    @AuthPermissions("demo:test:edit")
    @Put
    @Mapping
    public Result<Void> edit(@Body @Validated DemoTest demoTest) {
        return toResult(demoTestService.updateById(demoTest));
    }

    /**
     * 删除演示
     */
    @QzLog(title = "演示", businessType = BusinessType.DELETE)
    @AuthPermissions("demo:test:remove")
    @Delete
    @Mapping("{ids}")
    public Result<Void> delete(@Path Long[] ids) {
        return toResult(demoTestService.removeByIds(Arrays.asList(ids)));
    }

    private QueryWrapper getQW(DemoTest demoTest) {
        QueryWrapper qw = QueryWrapper.create();
        qw.and(DemoTest::getName).like(demoTest.getName());
        qw.and(DemoTest::getAge).eq(demoTest.getAge());
        qw.and(DemoTest::getBirthday).eq(demoTest.getBirthday());
        qw.and(DemoTest::getLiveAddress).eq(demoTest.getLiveAddress());
        return qw;
    }

}
